﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Website.Interfaces;

namespace Website.Infrastructure
{
    public class AuthorizedUserManager : IAuthorizedUserManager
    {
        private IEnumerable<string> _authorizedUsers;

        public AuthorizedUserManager(string dataPath)
        {
            _authorizedUsers = ReadAuthorizedUsers(dataPath);
        }

        public bool IsAllowedUser(string email)
        {
            return _authorizedUsers.Contains(email);
        }

        private IEnumerable<string> ReadAuthorizedUsers(string filePath)
        {
            var emails = from line in File.ReadAllLines(Path.Combine(filePath, "AuthorizedUsers.txt"))
                         where !string.IsNullOrWhiteSpace(line)
                         select line.Trim();

            return emails;
        }
    }
}